package com.itlzh.mall.user.service.impl;

import com.itlzh.mall.common.entity.UserInfo;
import com.itlzh.mall.user.service.ILoginService;
import com.itlzh.mall.user.service.IUserInfoService;
import com.itlzh.mall.user.service.IUserLoginLogService;
import com.itlzh.mall.common.dto.LoginDTO;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginServiceImpl implements ILoginService {
    @Autowired
    private IUserInfoService iUserInfoService;
    @Autowired
    private IUserLoginLogService  iUserLoginLogService;

    /**
     * 用户登录
     * @param loginDTO
     * @return
     */
    @Override
    public UserInfo login(LoginDTO loginDTO) {
        UserInfo userInfo = iUserInfoService.queryByLoginName(loginDTO.getUsername());
        if(userInfo==null){
            throw new RuntimeException("该用户不存在");
        }
        if(!BCrypt.checkpw(loginDTO.getPassword(),userInfo.getPassword()))
            throw new RuntimeException("密码错误");
        iUserLoginLogService.writeLoginLog(userInfo.getId());
        return userInfo;
    }
}
